import { rectNodeConfig } from '@/credit/components/flow/element/theme'
import { echartsXFormatter } from '@vimi/utils-tool'
// const horSpace = 50
const verSpace = 35
const startY = 50
const spaceH = rectNodeConfig.height + verSpace
// const spaceW = rectNodeConfig.width + horSpace

const branch2Flow = (branchData, nodes, edges, { startN, startX, deep = '', endNodeId }) => {
  let nodeN = startN
  let branchMaxN = nodeN
  const getId = (key, cond) => (deep ? `${deep}-` : '') + key + cond
  const nodeId = getId(branchData.wfn_key, branchData.branch_cond)
  const nextNodes = branchData.next_nodes || []

  if (nextNodes.length > 1) {
    nodeN++
  }
  if (!branchData.next_nodes || !branchData.next_nodes.length) {
    const data = edges.find(item => item.sourceNodeId === nodeId && item.targetNodeId === endNodeId) // 后端所给数据需去除重复的线
    if (!data) {
      edges.push({
        type: 'flow:polyline',
        sourceNodeId: nodeId,
        targetNodeId: endNodeId,
      })
    }
  }
  nextNodes.forEach((item, i) => {
    const nextId = item.wfn_key ? getId(item.wfn_key, item.branch_cond) : endNodeId
    const exNode = nodes.find(item => item.id === nextId) || {}
    if (!exNode.id) {
      nodes.push({
        id: nextId,
        x: nextNodes.length > 1 && i === 0 ? startX - 130 : startX + i * 130,
        y: startY + spaceH * nodeN,
        type: 'flow:user',
        text: item.wfn_name,
        properties: {
          icon: item.wfn_key === 'PROJECT_APPROVAL_DIR-JURY' ? 'group' : 'user',
          type: 'primary',
          key: item.wfn_key,
        },
      })
    }
    const edgesExist = edges.find(
      item => item.sourceNodeId === nodeId && item.targetNodeId === nextId
    ) // 后端所给数据需去除重复的线
    if (!edgesExist) {
      edges.push({
        type: 'flow:polyline',
        sourceNodeId: nodeId,
        targetNodeId: nextId,
        text: nextNodes.length > 1 ? echartsXFormatter(item.branch_cond || '', 8) : '',
      })
    }

    const branchN = branch2Flow(item, nodes, edges, {
      startN: nodeN + 1,
      startX: nextNodes.length > 1 && i === 0 ? startX - 130 : startX + i * 130,
      deep: '',
      endNodeId,
    })
    branchMaxN = Math.max(branchMaxN, branchN)
  })
  return branchMaxN
}
// 分支递归函数，设置节点和边
// const branch2Flow = (branchData, nodes, edges, { startN, startX, deep = '', endNodeId }) => {
//   let nodeN = startN
//   const nodesLen = branchData.length

//   const getId = key => (deep ? `${deep}-` : '') + key

//   for (let i = 0; i < nodesLen; i++) {
//     const nodeData = branchData[i]
//     const isOneApproval = +nodeData.is_one_approval === 1
//     const isBranchNode = +nodeData.is_branch_node === 1
//     const nextBranchList = nodeData.branch_info
//     const nodeId = getId(nodeData.wfn_key)
//     const nextId = i < nodesLen - 1 ? getId(branchData[i + 1].wfn_key) : endNodeId
//     const properties = {
//       icon: isOneApproval ? 'user' : 'group',
//       type: 'primary',
//       key: nodeData.wfn_key,
//     }
//     nodes.push({
//       id: nodeId,
//       x: startX,
//       y: startY + spaceH * nodeN,
//       type: 'flow:user',
//       text: nodeData.wfn_name,
//       properties,
//     })
//     edges.push({
//       type: 'flow:polyline',
//       sourceNodeId: nodeId,
//       targetNodeId: nextId,
//       properties,
//     })
//     nodeN++

//     // 分支
//     if (isBranchNode && Array.isArray(nextBranchList) && nextBranchList.length) {
//       // 分支的边和节点
//       const getewayId = `node-${nodeN}`
//       const gatewayY = startY + spaceH * nodeN
//       edges[edges.length - 1].targetNodeId = getewayId
//       nodes.push({
//         id: getewayId,
//         x: startX,
//         y: gatewayY,
//         type: 'flow:gateway',
//         properties,
//       })

//       nodeN++

//       // 分支位置
//       const branchCount = nextBranchList.length
//       const eachWidth = (spaceW - horSpace / branchCount) * 2.5
//       let branchMaxN = 0
//       for (let j = 0; j < branchCount; j++) {
//         const branchInfo = nextBranchList[j]
//         const branchDeep = (deep ? `${deep}.` : '') + j
//         const nextBranchNodes = branchInfo.branch_nodes || []
//         const branchTargetId = nextBranchNodes.length
//           ? `${branchDeep}-${nextBranchNodes[0].wfn_key}`
//           : endNodeId
//         edges.push({
//           type: 'flow:polyline',
//           sourceNodeId: getewayId,
//           targetNodeId: branchTargetId,
//           text: branchInfo.condFieldKeyName,
//           properties,
//         })
//         const branchN = branch2Flow(nextBranchNodes, nodes, edges, {
//           startN: nodeN,
//           startX: startX + (j - branchCount / 2) * eachWidth + eachWidth / 2,
//           deep: branchDeep,
//           endNodeId: nextId,
//         })
//         branchMaxN = Math.max(branchMaxN, branchN)
//       }

//       nodeN = branchMaxN
//     }
//   }

//   return nodeN
// }

export const wfn2Flow = (flowData, width = 800) => {
  const startX = width / 2
  const startNodeId = 'start_node'
  const endNodeId = 'end_node'

  const nodes = []
  const edges = []

  nodes.push({
    type: 'flow:start',
    id: 'start_node',
    x: startX,
    y: startY + 10,
    text: '开始',
    properties: {
      textPosition: 'up',
    },
  })
  nodes.push({
    id: flowData.wfn_key + flowData.branch_cond,
    x: startX,
    y: startY + spaceH,
    type: 'flow:user',
    text: flowData.wfn_name,
    properties: {
      icon: 'user',
      type: 'primary',
      key: flowData.wfn_key,
    },
  })
  edges.push({
    type: 'flow:polyline',
    sourceNodeId: startNodeId,
    targetNodeId: flowData.wfn_key + flowData.branch_cond,
  })

  // const nodeN = branch2Flow(flowData, nodes, edges, {
  //   startN: 1,
  //   startX,
  //   endNodeId,
  // })
  const nodeN = branch2Flow(flowData, nodes, edges, {
    startN: 2,
    startX,
    endNodeId,
  })
  nodes.push({
    type: 'flow:end',
    id: endNodeId,
    x: startX,
    y: startY + spaceH * nodeN,
    text: '结束',
    properties: nodes[nodes.length - 1].properties,
  })

  return {
    nodes,
    edges,
  }
}
